package com.wn.servlet.demo03;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yellow Docter
 * @date 2022 -06-28
 * @desc
 * 01 servletContext的作用
 *    1.可以实现 servlet中的数据共享
 *    2.可以搞全局配置
 *
 * 02 如何获取servletContext : 每个web程序只能有一个,该对象的生命周期，跟Tomcat同步！
 *    01 调用父类的方法
 *
 */
@WebServlet(urlPatterns = "/context02")
public class Context02Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ServletContext servletContext3 = getServletContext();
        System.out.println("servletContext3 = " + servletContext3.hashCode());

        String key1 = (String) servletContext3.getAttribute("key1");
        System.out.println("key1 = " + key1);

        Integer key2 = (Integer) servletContext3.getAttribute("key2");
        System.out.println("key2 = " + key2);

        Object key3 =  servletContext3.getAttribute("key3");
        System.out.println("key3 = " + key3);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
